package com.macro.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.PmsSiteMapper;
import com.macro.mall.model.PmsSite;
import com.macro.mall.service.PmsSiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * PmsSite的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class PmsSiteServiceImpl implements PmsSiteService {

    private boolean isDebugEnabled;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private PmsSiteMapper pmsSiteMapper;
    // TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化

    @Override
    public List<PmsSite> find(PmsSite value, Integer pageNum, Integer pageSize) {
        //TODO这里可以做通过Assist做添加查询
        List<PmsSite> result = null;
        PageHelper.startPage(pageNum, pageSize);
        Assist assist = new Assist();
        try {
            if (null != value.getDeptId()) {
                assist.andEq("dept_id", value.getDeptId());
            }
            if (StrUtil.isNotEmpty(value.getSiteName())) {
                assist.andLike("site_name", "%" + value.getSiteName() + "%");
            }
            /**01-可用 02-禁用*/
            if(StrUtil.isNotEmpty(value.getStatus())) {
                assist.andLike("status", value.getStatus());
            }
            assist.setOrder(Assist.order("create_time", false));
            result = pmsSiteMapper.selectPmsSite(assist);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取PmsSite数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public PmsSite findOne(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过PmsSite的id获得PmsSite对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过PmsSite的id获得PmsSite对象-->失败:id不能为空");
        }
        PmsSite result = null;
        try {
            result = pmsSiteMapper.selectPmsSiteById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过PmsSite的id获得PmsSite对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(PmsSite value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将PmsSite中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        if (!StrUtil.isBlankIfStr(value.getEndTime()) && !StrUtil.isBlankIfStr(value.getStartTime())) {
            if (value.getEndTime().getTime() < value.getStartTime().getTime()) {
                Asserts.fail("合作开始时间应早于结束时间");
            }
            if(StrUtil.isBlank(value.getStatus())) {
                long l = System.currentTimeMillis();
                if (value.getStartTime().getTime() < l && value.getEndTime().getTime() > l) {
                    value.setStatus("01");
                } else {
                    value.setStatus("02");
                }
            }
        }
        int result = 0;
        try {
            result = pmsSiteMapper.insertNotNullPmsSite(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行将PmsSite中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(PmsSite value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过PmsSite的id更新PmsSite中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过PmsSite的id更新PmsSite中属性不为null的数据-->失败:对象为null");
        }
        int result = 0;
        if (!StrUtil.isBlankIfStr(value.getEndTime()) && !StrUtil.isBlankIfStr(value.getStartTime())) {
            if (value.getEndTime().getTime() < value.getStartTime().getTime()) {
                Asserts.fail("合作开始时间应早于结束时间");
            }
            if(StrUtil.isBlank(value.getStatus())) {
                long l = System.currentTimeMillis();
                if (value.getStartTime().getTime() < l && value.getEndTime().getTime() > l) {
                    value.setStatus("01");
                } else {
                    value.setStatus("02");
                }
            }
        }
        try {
            result = pmsSiteMapper.updateNotNullPmsSiteById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过PmsSite的id更新PmsSite中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过PmsSite的id删除PmsSite-->失败:id不能为空");
            }
            Asserts.fail("执行通过PmsSite的id删除PmsSite-->失败:id不能为空");
        }
        int result = 0;
        try {
            result = pmsSiteMapper.deletePmsSiteById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过PmsSite的id删除PmsSite-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public Integer count(Integer id) {
        Assist assist = new Assist();
        assist.andEq("dept_Id  ", id);
        assist.andEq("site_type  ", 2);
        assist.andEq("status  ", "01");
        Long pmsSiteRowCount = pmsSiteMapper.getPmsSiteRowCount(assist);
        return pmsSiteRowCount.intValue();
    }


}